//
//  BCBaseVC.swift
//  NewXueBa
//
//  Created by Shine on 2019/5/30.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import UIKit

class BCBaseVC: UIViewController {
    
    var bcTabView: UIScrollView? {
        didSet {
            if (bcTabView?.isKind(of: UITableView.self))! {
                let tab = bcTabView as? UITableView
                tab?.tableFooterView = UIView()
            }
            //如果修改绑定别的类型的表头/表尾 需要重新绑定新的方法
//            bcTabView?.bcHead = BCRefreshHeader { [weak self] in self?.lodeData() }
//            bcTabView?.bcFoot = BCRefreshFooter(refreshingBlock: { [weak self] in self?.loadMoreData() })
            
        }
    }
    
    var isBaseLoading: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 解决ios11 由于安全区域safeArea问题造成的表的向下偏移
        if #available(iOS 11.0, *) {
            UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        configUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    func configUI() {}
    
    func emptyLoading() { lodeData() }
    
    @objc func lodeData()  {}
    @objc func loadMoreData()  {}
    
}

extension UIViewController {
    
    func showTipMsg(_ msg: String ,time: Double)  {
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.mode = .text
        hud.label.text = msg
        hud.margin     = 10 // HUD各元素与HUD边缘的间距
        hud.offset.y   = -66 // HUD相对于父视图中心点的x轴偏移量和y轴偏移量
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: time)
    }
    
    class func showTipMsgInWindow(_ msg: String ,time: Double)  {
        guard let rootView =  UIApplication.shared.delegate?.window??.rootViewController?.view else { return }
        let hud = MBProgressHUD.showAdded(to: rootView, animated: true)
        hud.mode = .text
        hud.label.text = msg
        hud.margin     = 10 // HUD各元素与HUD边缘的间距
        hud.offset.y   = -66 // HUD相对于父视图中心点的x轴偏移量和y轴偏移量
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: time)
    }
    //显示菊花框
    func showTip(msg : String){
        MBProgressHUD.showAdded(to: self.view, animated: true).label.text = msg
    }
    //隐藏菊花框
    func hidenTip(){
        MBProgressHUD.hide(for: self.view, animated: true)
    }
    
    /// 前往的控制器
    ///
    /// - Parameter animated: 是否做动画
    func bc_backToViewController(animated: Bool) {
        if self.navigationController != nil {
            if self.navigationController?.viewControllers.count == 1 {
                self.dismiss(animated: animated, completion: nil)
            } else {
                self.navigationController?.popViewController(animated: animated)
            }
            
        } else if self.presentingViewController != nil {
            self.dismiss(animated: animated, completion: nil)
        }
    }
    
    /// 当前的控制器
    ///
    /// - Returns: 控制器
    class func bc_currentViewController() -> UIViewController {
        if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
            return self.bc_currentViewController(form: rootVC)
        } else {
            return UIViewController()
        }
    }
    
    /// 根据根控制器的类型返回当前的控制器
    ///
    /// - Parameter fromVC: 根控制器
    /// - Returns: 返回的控制器
    class func bc_currentViewController(form fromVC: UIViewController) -> UIViewController {
        if fromVC.isKind(of: UINavigationController.self) {
            let navigationController = fromVC as! UINavigationController
            return bc_currentViewController(form: navigationController.viewControllers.last!)
        }
        else if fromVC.isKind(of: UITabBarController.self) {
            let tabBarController = fromVC as! UITabBarController
            return bc_currentViewController(form: tabBarController.selectedViewController!)
        }
        else if fromVC.presentedViewController != nil {
            return bc_currentViewController(form: fromVC.presentingViewController!)
        }
        else {
            return fromVC
        }
    }
}

